package com

import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.ObjectUtils
import com.blankj.utilcode.util.SPUtils
import com.shunhao.network.entity.UserInfoBean

/**
 * ClassName:UserManager
 * Package:com.shunhao.medicale.util
 * Description:
 * Com:shunHao.com
 * @date:2020/11/16 10:06
 * @author:827746955@qq.com
 */
object UserManagerCenter {

    private const val SP_KEY_USER_ID = "USER_ID"
    private const val LOGIN_USER_INFO_KEY = "LOGIN_USER_INFO_KEY"
    const val IS_HAS_NEW_VERSION = "IS_HAS_NEW_VERSION"
    private const val ACCESS_TOKEN_KEY = "ACCESS_TOKEN_KEY"
    private const val KEY_CACHE_PSD = "PSD"
    private const val KEY_CACHE_USERNAME = "USER_NAME"
    const val HISTORY_SEARCH_CONTENT = "HISTORY_SEARCH_CONTENT"

    private var sUserId = 0
    private var isUserIdLoaded = false

    private var sToken = ""
    private var isTokenLoaded = false

    private var isUserInfoBeanLoaded = false
    private var mUserInfoBean: UserInfoBean? = null

    private var isUserNameLoaded = false
    private var mUserName = ""

    private var isPsdLoaded = false
    private var mPsd = ""

    /**
     * 判断是否登录
     */
    @JvmStatic
    fun isLogin(): Boolean {
        return getUserId() > 0
    }

    /**
     * 获取用户信息对象
     */
    @JvmStatic
    fun getUserInfoBean(): UserInfoBean? {
        if (isUserInfoBeanLoaded) {
            return mUserInfoBean
        }
        isUserInfoBeanLoaded = true
        val userData = SPUtils.getInstance().getString(LOGIN_USER_INFO_KEY)
        if (ObjectUtils.isEmpty(userData)) {
            return null
        }
        mUserInfoBean = GsonUtils.fromJson(userData, UserInfoBean::class.java)
        return mUserInfoBean
    }

    /**
     * 保存用户信息对象
     */
    @JvmStatic
    fun saveUserInfoBean(userInfoBean: UserInfoBean) {
        isUserInfoBeanLoaded = true
        val userData = GsonUtils.toJson(userInfoBean)
        SPUtils.getInstance().put(LOGIN_USER_INFO_KEY, userData)
        mUserInfoBean = userInfoBean
    }


    /**
     * 获取登录userId
     */
    @JvmStatic
    fun getUserId(): Int {
        if (isUserIdLoaded) {
            return sUserId
        }
        isUserIdLoaded = true
        sUserId = SPUtils.getInstance().getInt(SP_KEY_USER_ID)
        return sUserId
    }

    /**
     * 储存登录id
     */
    @JvmStatic
    fun saveUserId(userId: Int) {
        isUserIdLoaded = true
        SPUtils.getInstance().put(SP_KEY_USER_ID, userId)
        sUserId = userId;
    }

    /**
     * 储存token
     */
    @JvmStatic
    fun saveToken(token: String) {
        isTokenLoaded = true
        SPUtils.getInstance().put(ACCESS_TOKEN_KEY, token)
        sToken = token
    }

    @JvmStatic
    fun getToken(): String {
        if (isTokenLoaded) {
            return sToken
        }
        isTokenLoaded = true
        sToken = SPUtils.getInstance().getString(ACCESS_TOKEN_KEY)
        return sToken
    }

    /**
     * 保存密码 、 极光用
     */
    @JvmStatic
    fun savePsd(psd: String) {
        isPsdLoaded = true
        SPUtils.getInstance().put(KEY_CACHE_PSD, psd)
        mPsd = psd
    }

    /**
     * 保存密码
     */
    @JvmStatic
    fun getPsd(): String {
        if (isPsdLoaded) {
            return mPsd
        }
        isPsdLoaded = true
        mPsd = SPUtils.getInstance().getString(KEY_CACHE_PSD)
        return mPsd
    }

    /**
     * 保存极光userName 、 极光用
     */
    @JvmStatic
    fun saveUserName(userName: String) {
        isUserNameLoaded = true
        SPUtils.getInstance().put(KEY_CACHE_USERNAME, userName)
        mUserName = userName
    }

    /**
     * 获取userName
     */
    @JvmStatic
    fun getUserName(): String {
        if (isUserNameLoaded) {
            return mUserName
        }
        isUserNameLoaded = true
        mUserName = SPUtils.getInstance().getString(KEY_CACHE_USERNAME)
        return mUserName
    }

}